/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.arrye.Flickr;

import java.io.DataInputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import mjs.microrest.*;
import org.netbeans.microedition.lcdui.SplashScreen;
import org.netbeans.microedition.lcdui.WaitScreen;
import org.netbeans.microedition.util.SimpleCancellableTask;

/**
 * @author Wilson.ARG
 */
public class Browser extends MIDlet implements CommandListener {

    private boolean midletPaused = false;
    private int pageNumber = 0;
    private int imagesPerPage = 9;
    private String error = null;
    private String apiKey = "e8574343d575476a4a9db865b62745ad";
    private Image[] images = null;
    private RestResult[] photos = null;

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private SplashScreen splashScreen;
    private Form searchForm;
    private TextField tagField;
    private WaitScreen waitScreen;
    private Alert alert;
    private ImageList imageList;
    private List selectOptionList;
    private ImageViewer imageViewer;
    private Command backCommand;
    private Command searchCommand;
    private Command exitCommand;
    private Command cancelCommand;
    private Command viewCommand;
    private Command nextPageCommand;
    private Command prevPageCommand;
    private Command backCommand1;
    private SimpleCancellableTask task;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The Browser constructor.
     */
    public Browser() {
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getSplashScreen());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == imageList) {//GEN-BEGIN:|7-commandAction|1|68-preAction
            if (command == backCommand) {//GEN-END:|7-commandAction|1|68-preAction
                // write pre-action user code here
                switchDisplayable(null, getSearchForm());//GEN-LINE:|7-commandAction|2|68-postAction
                // write post-action user code here
            } else if (command == exitCommand) {//GEN-LINE:|7-commandAction|3|65-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|4|65-postAction
                // write post-action user code here
            } else if (command == nextPageCommand) {//GEN-LINE:|7-commandAction|5|72-preAction
                // write pre-action user code here
                goNextPage();//GEN-LINE:|7-commandAction|6|72-postAction
                // write post-action user code here
            } else if (command == prevPageCommand) {//GEN-LINE:|7-commandAction|7|74-preAction
                // write pre-action user code here
                goPrevPage();//GEN-LINE:|7-commandAction|8|74-postAction
                // write post-action user code here
            } else if (command == viewCommand) {//GEN-LINE:|7-commandAction|9|64-preAction
                // write pre-action user code here
                showImage();//GEN-LINE:|7-commandAction|10|64-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|11|105-preAction
        } else if (displayable == imageViewer) {
            if (command == backCommand) {//GEN-END:|7-commandAction|11|105-preAction
                // write pre-action user code here
                switchDisplayable(null, getImageList());//GEN-LINE:|7-commandAction|12|105-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|13|23-preAction
        } else if (displayable == searchForm) {
            if (command == exitCommand) {//GEN-END:|7-commandAction|13|23-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|14|23-postAction
                // write post-action user code here
            } else if (command == searchCommand) {//GEN-LINE:|7-commandAction|15|20-preAction
                // write pre-action user code here
                pageNumber = 1;
                switchDisplayable(null, getWaitScreen());//GEN-LINE:|7-commandAction|16|20-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|17|89-preAction
        } else if (displayable == selectOptionList) {
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|17|89-preAction
                // write pre-action user code here
                selectOptionListAction();//GEN-LINE:|7-commandAction|18|89-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|19|15-preAction
        } else if (displayable == splashScreen) {
            if (command == SplashScreen.DISMISS_COMMAND) {//GEN-END:|7-commandAction|19|15-preAction
                // write pre-action user code here
                switchDisplayable(null, getSelectOptionList());//GEN-LINE:|7-commandAction|20|15-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|21|29-preAction
        } else if (displayable == waitScreen) {
            if (command == WaitScreen.FAILURE_COMMAND) {//GEN-END:|7-commandAction|21|29-preAction
                // write pre-action user code here
                switchDisplayable(getAlert(), getSearchForm());//GEN-LINE:|7-commandAction|22|29-postAction
                // write post-action user code here
            } else if (command == WaitScreen.SUCCESS_COMMAND) {//GEN-LINE:|7-commandAction|23|28-preAction
                // write pre-action user code here
                switchDisplayable(null, getImageList());//GEN-LINE:|7-commandAction|24|28-postAction
                // write post-action user code here
            } else if (command == cancelCommand) {//GEN-LINE:|7-commandAction|25|49-preAction
                // write pre-action user code here
                switchDisplayable(null, getSearchForm());//GEN-LINE:|7-commandAction|26|49-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|27|7-postCommandAction
        }//GEN-END:|7-commandAction|27|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|28|
    //</editor-fold>//GEN-END:|7-commandAction|28|


    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: splashScreen ">//GEN-BEGIN:|13-getter|0|13-preInit
    /**
     * Returns an initiliazed instance of splashScreen component.
     * @return the initialized component instance
     */
    public SplashScreen getSplashScreen() {
        if (splashScreen == null) {//GEN-END:|13-getter|0|13-preInit
            // write pre-init user code here
            splashScreen = new SplashScreen(getDisplay());//GEN-BEGIN:|13-getter|1|13-postInit
            splashScreen.setTitle("Flickr Browser");
            splashScreen.setCommandListener(this);
            splashScreen.setFullScreenMode(true);
            splashScreen.setText("Arr-Ye! Labs");//GEN-END:|13-getter|1|13-postInit
            // write post-init user code here
        }//GEN-BEGIN:|13-getter|2|
        return splashScreen;
    }
    //</editor-fold>//GEN-END:|13-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: searchForm ">//GEN-BEGIN:|17-getter|0|17-preInit
    /**
     * Returns an initiliazed instance of searchForm component.
     * @return the initialized component instance
     */
    public Form getSearchForm() {
        if (searchForm == null) {//GEN-END:|17-getter|0|17-preInit
            // write pre-init user code here
            searchForm = new Form("Search", new Item[] { getTagField() });//GEN-BEGIN:|17-getter|1|17-postInit
            searchForm.addCommand(getSearchCommand());
            searchForm.addCommand(getExitCommand());
            searchForm.setCommandListener(this);//GEN-END:|17-getter|1|17-postInit
            // write post-init user code here
        }//GEN-BEGIN:|17-getter|2|
        return searchForm;
    }
    //</editor-fold>//GEN-END:|17-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: tagField ">//GEN-BEGIN:|18-getter|0|18-preInit
    /**
     * Returns an initiliazed instance of tagField component.
     * @return the initialized component instance
     */
    public TextField getTagField() {
        if (tagField == null) {//GEN-END:|18-getter|0|18-preInit
            // write pre-init user code here
            tagField = new TextField("Tag:", "", 32, TextField.ANY);//GEN-LINE:|18-getter|1|18-postInit
            // write post-init user code here
        }//GEN-BEGIN:|18-getter|2|
        return tagField;
    }
    //</editor-fold>//GEN-END:|18-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: waitScreen ">//GEN-BEGIN:|25-getter|0|25-preInit
    /**
     * Returns an initiliazed instance of waitScreen component.
     * @return the initialized component instance
     */
    public WaitScreen getWaitScreen() {
        if (waitScreen == null) {//GEN-END:|25-getter|0|25-preInit
            // write pre-init user code here
            waitScreen = new WaitScreen(getDisplay());//GEN-BEGIN:|25-getter|1|25-postInit
            waitScreen.setTitle("Wait...");
            waitScreen.addCommand(getCancelCommand());
            waitScreen.setCommandListener(this);
            waitScreen.setText("Processing");
            waitScreen.setTask(getTask());//GEN-END:|25-getter|1|25-postInit
            // write post-init user code here
        }//GEN-BEGIN:|25-getter|2|
        return waitScreen;
    }
    //</editor-fold>//GEN-END:|25-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: alert ">//GEN-BEGIN:|33-getter|0|33-preInit
    /**
     * Returns an initiliazed instance of alert component.
     * @return the initialized component instance
     */
    public Alert getAlert() {
        if (alert == null) {//GEN-END:|33-getter|0|33-preInit
            // write pre-init user code here
            alert = new Alert("Error!", error, null, AlertType.ERROR);//GEN-BEGIN:|33-getter|1|33-postInit
            alert.setTimeout(Alert.FOREVER);//GEN-END:|33-getter|1|33-postInit
            // write post-init user code here
        }//GEN-BEGIN:|33-getter|2|
        return alert;
    }
    //</editor-fold>//GEN-END:|33-getter|2|








    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: searchCommand ">//GEN-BEGIN:|19-getter|0|19-preInit
    /**
     * Returns an initiliazed instance of searchCommand component.
     * @return the initialized component instance
     */
    public Command getSearchCommand() {
        if (searchCommand == null) {//GEN-END:|19-getter|0|19-preInit
            // write pre-init user code here
            searchCommand = new Command("Search", Command.OK, 0);//GEN-LINE:|19-getter|1|19-postInit
            // write post-init user code here
        }//GEN-BEGIN:|19-getter|2|
        return searchCommand;
    }
    //</editor-fold>//GEN-END:|19-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|22-getter|0|22-preInit
    /**
     * Returns an initiliazed instance of exitCommand component.
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {//GEN-END:|22-getter|0|22-preInit
            // write pre-init user code here
            exitCommand = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|22-getter|1|22-postInit
            // write post-init user code here
        }//GEN-BEGIN:|22-getter|2|
        return exitCommand;
    }
    //</editor-fold>//GEN-END:|22-getter|2|





    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cancelCommand ">//GEN-BEGIN:|48-getter|0|48-preInit
    /**
     * Returns an initiliazed instance of cancelCommand component.
     * @return the initialized component instance
     */
    public Command getCancelCommand() {
        if (cancelCommand == null) {//GEN-END:|48-getter|0|48-preInit
            // write pre-init user code here
            cancelCommand = new Command("Cancel", Command.CANCEL, 0);//GEN-LINE:|48-getter|1|48-postInit
            // write post-init user code here
        }//GEN-BEGIN:|48-getter|2|
        return cancelCommand;
    }
    //</editor-fold>//GEN-END:|48-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: task ">//GEN-BEGIN:|30-getter|0|30-preInit
    /**
     * Returns an initiliazed instance of task component.
     * @return the initialized component instance
     */
    public SimpleCancellableTask getTask() {
        if (task == null) {//GEN-END:|30-getter|0|30-preInit
            // write pre-init user code here
            task = new SimpleCancellableTask();//GEN-BEGIN:|30-getter|1|30-execute
            task.setExecutable(new org.netbeans.microedition.util.Executable() {
                public void execute() throws Exception {//GEN-END:|30-getter|1|30-execute
                    // write task-execution user code here
                    String tag = tagField.getString().toLowerCase();
                    waitScreen.setText("Search '"+tag+"' page "+pageNumber);
                    RestRequest req = new RestRequest("http://api.flickr.com/services/rest/");
                    req.setParameter("method", "flickr.photos.search");
                    req.setParameter("api_key", apiKey);
                    //req.setParameter("api_sig", "d6ab5e2e5ef921c5f51f605702e6381c");
                    req.setParameter("tags", tag);
                    req.setParameter("privacy_filter", "1");
                    req.setParameter("safe_search", "3");
                    req.setParameter("media", "photos");
                    req.setParameter("per_page", String.valueOf(imagesPerPage));
                    req.setParameter("page", String.valueOf(pageNumber));
                    RestResponse resp = req.sendAndWait();
                    photos = resp.getResults("/rsp/photos/photo");
                    waitScreen.setText("Obtained "+photos.length+" results!\n");
                    images = new Image[imagesPerPage];
                    for (int i = 0; i < photos.length; i++) {
                        RestResult photo = photos[i];
                        waitScreen.setText("Fetching result #"+(i+1));
                        images[i] = getImage(photo, "s");
                    }
                    getImageList().Initialize(images, 75, 75, "Page "+pageNumber+" of "+resp.get("/rsp/photos/@pages"));
                    if (pageNumber == 1)
                        getImageList().removeCommand(prevPageCommand);
                    if (String.valueOf(pageNumber).compareTo(resp.get("/rsp/photos/@pages")) == 0) {
                        getImageList().removeCommand(nextPageCommand);
                    }
                }//GEN-BEGIN:|30-getter|2|30-postInit
            });//GEN-END:|30-getter|2|30-postInit
            // write post-init user code here
        }//GEN-BEGIN:|30-getter|3|
        return task;
    }
    //</editor-fold>//GEN-END:|30-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: imageList ">//GEN-BEGIN:|62-getter|0|62-preInit
    /**
     * Returns an initiliazed instance of imageList component.
     * @return the initialized component instance
     */
    public ImageList getImageList() {
        if (imageList == null) {//GEN-END:|62-getter|0|62-preInit
            // write pre-init user code here
            imageList = new ImageList();//GEN-BEGIN:|62-getter|1|62-postInit
            imageList.setTitle("imageList");
            imageList.addCommand(getViewCommand());
            imageList.addCommand(getExitCommand());
            imageList.addCommand(getBackCommand());
            imageList.addCommand(getNextPageCommand());
            imageList.addCommand(getPrevPageCommand());
            imageList.setCommandListener(this);//GEN-END:|62-getter|1|62-postInit
            // write post-init user code here
        }//GEN-BEGIN:|62-getter|2|
        return imageList;
    }
    //</editor-fold>//GEN-END:|62-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: viewCommand ">//GEN-BEGIN:|63-getter|0|63-preInit
    /**
     * Returns an initiliazed instance of viewCommand component.
     * @return the initialized component instance
     */
    public Command getViewCommand() {
        if (viewCommand == null) {//GEN-END:|63-getter|0|63-preInit
            // write pre-init user code here
            viewCommand = new Command("View", Command.OK, 0);//GEN-LINE:|63-getter|1|63-postInit
            // write post-init user code here
        }//GEN-BEGIN:|63-getter|2|
        return viewCommand;
    }
    //</editor-fold>//GEN-END:|63-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand ">//GEN-BEGIN:|67-getter|0|67-preInit
    /**
     * Returns an initiliazed instance of backCommand component.
     * @return the initialized component instance
     */
    public Command getBackCommand() {
        if (backCommand == null) {//GEN-END:|67-getter|0|67-preInit
            // write pre-init user code here
            backCommand = new Command("Back", Command.BACK, 0);//GEN-LINE:|67-getter|1|67-postInit
            // write post-init user code here
        }//GEN-BEGIN:|67-getter|2|
        return backCommand;
    }
    //</editor-fold>//GEN-END:|67-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: nextPageCommand ">//GEN-BEGIN:|71-getter|0|71-preInit
    /**
     * Returns an initiliazed instance of nextPageCommand component.
     * @return the initialized component instance
     */
    public Command getNextPageCommand() {
        if (nextPageCommand == null) {//GEN-END:|71-getter|0|71-preInit
            // write pre-init user code here
            nextPageCommand = new Command("Next Page", Command.ITEM, 0);//GEN-LINE:|71-getter|1|71-postInit
            // write post-init user code here
        }//GEN-BEGIN:|71-getter|2|
        return nextPageCommand;
    }
    //</editor-fold>//GEN-END:|71-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: prevPageCommand ">//GEN-BEGIN:|73-getter|0|73-preInit
    /**
     * Returns an initiliazed instance of prevPageCommand component.
     * @return the initialized component instance
     */
    public Command getPrevPageCommand() {
        if (prevPageCommand == null) {//GEN-END:|73-getter|0|73-preInit
            // write pre-init user code here
            prevPageCommand = new Command("Prev Page", Command.ITEM, 0);//GEN-LINE:|73-getter|1|73-postInit
            // write post-init user code here
        }//GEN-BEGIN:|73-getter|2|
        return prevPageCommand;
    }
    //</editor-fold>//GEN-END:|73-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: goNextPage ">//GEN-BEGIN:|79-entry|0|80-preAction
    /**
     * Performs an action assigned to the goNextPage entry-point.
     */
    public void goNextPage() {//GEN-END:|79-entry|0|80-preAction
        // write pre-action user code here
        pageNumber++;
        switchDisplayable(null, getWaitScreen());//GEN-LINE:|79-entry|1|80-postAction
        // write post-action user code here
    }//GEN-BEGIN:|79-entry|2|
    //</editor-fold>//GEN-END:|79-entry|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: goPrevPage ">//GEN-BEGIN:|84-entry|0|85-preAction
    /**
     * Performs an action assigned to the goPrevPage entry-point.
     */
    public void goPrevPage() {//GEN-END:|84-entry|0|85-preAction
        // write pre-action user code here
        pageNumber--;
        switchDisplayable(null, getWaitScreen());//GEN-LINE:|84-entry|1|85-postAction
        // write post-action user code here
    }//GEN-BEGIN:|84-entry|2|
    //</editor-fold>//GEN-END:|84-entry|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: selectOptionList ">//GEN-BEGIN:|88-getter|0|88-preInit
    /**
     * Returns an initiliazed instance of selectOptionList component.
     * @return the initialized component instance
     */
    public List getSelectOptionList() {
        if (selectOptionList == null) {//GEN-END:|88-getter|0|88-preInit
            // write pre-init user code here
            selectOptionList = new List("Select option", Choice.IMPLICIT);//GEN-BEGIN:|88-getter|1|88-postInit
            selectOptionList.append("Search", null);
            selectOptionList.append("Show user", null);
            selectOptionList.setCommandListener(this);
            selectOptionList.setSelectedFlags(new boolean[] { false, false });//GEN-END:|88-getter|1|88-postInit
            // write post-init user code here
        }//GEN-BEGIN:|88-getter|2|
        return selectOptionList;
    }
    //</editor-fold>//GEN-END:|88-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: selectOptionListAction ">//GEN-BEGIN:|88-action|0|88-preAction
    /**
     * Performs an action assigned to the selected list element in the selectOptionList component.
     */
    public void selectOptionListAction() {//GEN-END:|88-action|0|88-preAction
        // enter pre-action user code here
        String __selectedString = getSelectOptionList().getString(getSelectOptionList().getSelectedIndex());//GEN-BEGIN:|88-action|1|91-preAction
        if (__selectedString != null) {
            if (__selectedString.equals("Search")) {//GEN-END:|88-action|1|91-preAction
                // write pre-action user code here
                switchDisplayable(null, getSearchForm());//GEN-LINE:|88-action|2|91-postAction
                // write post-action user code here
            } else if (__selectedString.equals("Show user")) {//GEN-LINE:|88-action|3|92-preAction
                // write pre-action user code here
//GEN-LINE:|88-action|4|92-postAction
                // write post-action user code here
            }//GEN-BEGIN:|88-action|5|88-postAction
        }//GEN-END:|88-action|5|88-postAction
        // enter post-action user code here
    }//GEN-BEGIN:|88-action|6|
    //</editor-fold>//GEN-END:|88-action|6|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: showImage ">//GEN-BEGIN:|99-entry|0|100-preAction
    /**
     * Performs an action assigned to the showImage entry-point.
     */
    public void showImage() {//GEN-END:|99-entry|0|100-preAction
        // write pre-action user code here
        RestResult photo = photos[getImageList().getCurrent()];
        Image img = null;
        try {
            img = getImage(photo, "m");
        }
        catch (Exception ex) {
            error = ex.getMessage();
        }
        if (img != null) {
            getImageViewer().Initialize(img);
            getImageViewer().setTitle(photo.getAttributeValue("title"));
            switchDisplayable(null, getImageViewer());//GEN-LINE:|99-entry|1|100-postAction
        // write post-action user code here
        }
        else switchDisplayable(alert, getImageList());
    }//GEN-BEGIN:|99-entry|2|
    //</editor-fold>//GEN-END:|99-entry|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: imageViewer ">//GEN-BEGIN:|97-getter|0|97-preInit
    /**
     * Returns an initiliazed instance of imageViewer component.
     * @return the initialized component instance
     */
    public ImageViewer getImageViewer() {
        if (imageViewer == null) {//GEN-END:|97-getter|0|97-preInit
            // write pre-init user code here
            imageViewer = new ImageViewer();//GEN-BEGIN:|97-getter|1|97-postInit
            imageViewer.setTitle("imageViewer");
            imageViewer.addCommand(getBackCommand());
            imageViewer.setCommandListener(this);//GEN-END:|97-getter|1|97-postInit
            // write post-init user code here
        }//GEN-BEGIN:|97-getter|2|
        return imageViewer;
    }
    //</editor-fold>//GEN-END:|97-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand1 ">//GEN-BEGIN:|103-getter|0|103-preInit
    /**
     * Returns an initiliazed instance of backCommand1 component.
     * @return the initialized component instance
     */
    public Command getBackCommand1() {
        if (backCommand1 == null) {//GEN-END:|103-getter|0|103-preInit
            // write pre-init user code here
            backCommand1 = new Command("Back", Command.BACK, 0);//GEN-LINE:|103-getter|1|103-postInit
            // write post-init user code here
        }//GEN-BEGIN:|103-getter|2|
        return backCommand1;
    }
    //</editor-fold>//GEN-END:|103-getter|2|

    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet ();
        } else {
            initialize ();
            startMIDlet ();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }

    /**
     * Retrieves an image from flickr
     * @param id the image id
     * @return the image
     */
    private Image getImage(RestResult photo, String suffix) throws IOException, RestException {
        String url = "http://farm"+photo.getAttributeValue("farm")
                +".static.flickr.com/"+photo.getAttributeValue("server")
                +"/"+photo.getAttributeValue("id")+"_"
                +photo.getAttributeValue("secret")+(suffix.length() > 0?"_"+suffix:"")+".jpg";
        HttpConnection hcon = null;
        DataInputStream dis = null;
        Image img = null;
        boolean found = false;
        while (!found) {
            try {
                hcon = (HttpConnection)Connector.open(url);
                hcon.setRequestMethod(HttpConnection.GET);
                hcon.setRequestProperty("User-agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
                hcon.setRequestProperty("Accept", "image/jpg");
                hcon.setRequestProperty("Connection", "close");
                int status = hcon.getResponseCode();
                if (status == HttpConnection.HTTP_MOVED_PERM) {
                    url = hcon.getHeaderField("Location");
                    continue;
                }
                else {
                    if (status != HttpConnection.HTTP_OK) {
                        throw new IOException("Not found: "+status);
                    }
                    int len = (int)hcon.getLength();
                    if (len <= 0) {
                        throw new IOException("Content-length <= 0");
                    }
                    dis = hcon.openDataInputStream();
                    byte[] imgData = new byte[len];
                    dis.readFully(imgData);
                    img = Image.createImage(imgData, 0, len);
                    found = true;
                }
            }
            catch (IOException ex) {
                error = ex.getMessage();
                throw ex;
            }
            finally {
                if (dis != null) dis.close();
                if (hcon != null) hcon.close();
            }
        }
        return img;
    }
    
}
